<template>
    <Modal
        v-model="value"
        :width="500"
        :loading="fraudModalLoading"
        :closable="false"
        :styles="{top: '220px'}"
        :mask-closable='false'
    >
        <Spin fix size="large" v-if="fraudModalLoading">
            <slot name="loading"></slot>
        </Spin>
        <p slot="header" style="color:#f60">
            <Icon type="md-alert"></Icon>
            <span>欺诈反馈</span>
        </p>
        <Form :modal="fraudInfo" ref="fraudInfo" :rules="formRules" :label-width="120" >
            <FormItem prop="fraudTypeValue" label="欺诈类型" >
                <Select id="fraudTypeSelect" v-model="fraudInfo.fraudTypeValue"
                          :label-in-value="true"
                          clearable
                          @on-clear="clearFraudType"
                          @on-change="changeFraudType"
                          placement="bottom"
                >
                    <Option v-for="item in fraudTypeDic" :value="item.value">{{item.title}}</Option>
                </Select>
            </FormItem>
            <FormItem prop="dealerJoinValue" label="经销商是否参与">
                <Select v-model="fraudInfo.dealerJoinValue"
                        :label-in-value="true"
                        clearable
                        @on-clear="clearFraudDealerJoin"
                        @on-change="changeFraudDealerJoin"
                >
                    <Option :value="'yes'">是</Option>
                    <Option :value="'no'">否</Option>
                </Select>
            </FormItem>
            <FormItem prop="fraudDetailMsg" label="疑似欺诈案件详情" >
                <Input type="textarea" v-model="fraudInfo.fraudDetailMsg" :rows="4" style="width: 350px;height: 100px;line-height: 20px" />
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="primary" size="small" @click="cancelDiscoverFraud" ghost >取消</Button>
            <Button type="primary" size="small" @click="submitDiscoverFraud" >保存</Button>
        </div>
    </Modal>
</template>

<script>
    import {pushAntiFraud} from "../../../../api/afs-case/tortoise/tortoise";
    import {getDictDataByType} from "../../../../../basic/api/admin/datadic";

    export default {
        name: "tortoise-fraud-push",
        props :{
            applyNo:{
                type:String,
                default:""
            },
            callStep:{
                type:[String,Number],
                default:""
            },
            value:{
                type:Boolean,
                default: false,
            },
            fraudTypeKey:{
                type:String,
                default:"fraudType"
            }
        },
        data (){
            return {
                fraudInfo: {
                    applyNo: "",
                    fraudTypeValue: "",
                    fraudTypeTitle: "",
                    dealerJoinValue: "",
                    dealerJoinTitle: "",
                    fraudDetailMsg: "",
                    callStep:"",
                },
                fraudModalLoading:false,
                fraudTypeDic:[],
                formRules:{
                    fraudTypeValue:[{required:true,message:" ",trigger:"none"}],
                    dealerJoinValue:[{required:true,message:" ",trigger:"none"}],
                    fraudDetailMsg:[{required:true,message:" ",trigger:"none"}]
                }
            }
        },
        methods:{
            clearFraudDealerJoin() {
                this.fraudInfo.dealerJoinTitle = "";
            },
            changeFraudDealerJoin(item) {
                if(item) {
                    this.fraudInfo.dealerJoinTitle = item.label;
                }
            },
            clearFraudType() {
                this.fraudInfo.fraudTypeTitle = "";
            },
            changeFraudType(item) {
                if(item) {
                    this.fraudInfo.fraudTypeTitle = item.label;
                }
            },
            initFraudTypeDic() {
                if (this.fraudTypeDic.length == 0) {
                    getDictDataByType(this.fraudTypeKey).then(res => {
                        if (res.code == "0000") {
                            this.fraudTypeDic = res.data;
                        }
                    })
                }
            },
            cancelDiscoverFraud() {
                this.$emit("onClose");
            },
            submitDiscoverFraud() {
                if (!this.fraudInfo.fraudTypeValue) {
                    this.$Message.warning("请选择欺诈类型")
                    return;
                }
                if (!this.fraudInfo.dealerJoinValue) {
                    this.$Message.warning("请选择经销商是否参与")
                    return;
                }
                if (!this.fraudInfo.fraudDetailMsg.trim()) {
                    this.$Message.warning("请填写欺诈详情信息")
                    return;
                }
                this.$Modal.confirm({
                    title: "欺诈反馈",
                    content: "确认保存反欺诈信息?",
                    onOk: () => {
                        this.fraudModalLoading = true;
                        this.fraudInfo.applyNo = this.applyNo;
                        this.fraudInfo.callStep = this.callStep;
                        pushAntiFraud(this.fraudInfo).then(res => {
                            if (res.code == "0000") {
                                this.$Message.info("操作成功!");
                            }
                        }).finally(() => {
                            this.fraudModalLoading = false;
                            this.$emit("onClose",true,this.fraudInfo)
                        });
                    }
                })
            },
        },
        created() {
            this.initFraudTypeDic();
        },
        watch:{
            value(){
                if(this.value) {
                    this.fraudInfo.applyNo = "";
                    this.fraudInfo.fraudTypeValue = "";
                    this.fraudInfo.fraudTypeTitle = "";
                    this.fraudInfo.dealerJoinValue = "";
                    this.fraudInfo.dealerJoinTitle = "";
                    this.fraudInfo.fraudDetailMsg = "";
                    this.fraudInfo.callStep = "";
                }
            }
        }
    }
</script>

<style scoped>
    #fraudTypeSelect .ivu-select-dropdown-list{
        height: 200px;
    }
</style>
